package com.gkj.controller;

import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.dto.DataTablesDto;
import com.gkj.blog.business.dto.PageResult;
import com.gkj.blog.business.enums.ResponseStatus;
import com.gkj.blog.business.service.ArticleArchivesService;
import com.gkj.blog.business.service.ArticleService;
import com.gkj.blog.business.service.ArticleTagsService;
import com.gkj.blog.business.vo.condition.ArticleConditionVO;
import com.gkj.blog.business.vo.entity.ArticleVo;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.persistence.po.ArticleArchives;
import com.gkj.blog.util.ResultUtil;
import com.gkj.blog.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleTagsService articleTagsService;
    @RequestMapping("/list")
    public PageResult list(ArticleConditionVO vo) {
        PageInfo<ArticleVo> pageInfo = articleService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    /**
     * 管理自己的文章列表，与datatables前台借口配置
     * @param draw
     * @param vo
     * @return
     */
    @RequestMapping("/dataList")
    public DataTablesDto<ArticleVo> dataList(Integer draw,ArticleConditionVO vo){
        PageInfo<ArticleVo> pageInfo = articleService.findPageBreakByCondition(vo);
        if(pageInfo==null)
            return new DataTablesDto<ArticleVo>(draw,0L,0L,null);
        return new DataTablesDto<ArticleVo>(draw,pageInfo.getTotal(),pageInfo.getTotal(),pageInfo.getList());
    }

    @PostMapping("/remove")
    public ResponseVO remove(@RequestParam("ids[]") Long[] ids){
        if(articleService.deleteIds(ids))
            return ResultUtil.success(ResponseStatus.SUCCESS);
        else
            return ResultUtil.error("未选中任何内容");
    }

    @PostMapping("/recycle/{type}")
    public ResponseVO recycle(@PathVariable("type") String type,@RequestParam("ids[]") Long[] ids){
        if(type.equals("recover")){
            articleService.recover(ids);
        }else if(type.equals("add")){
            articleService.recycle(ids);
        }else{
            return ResultUtil.error("请求错误");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id){
        ArticleVo articleVo = articleService.select(id);
        return ResultUtil.success("success",articleVo);
    }

    @PostMapping("/save")
    public ResponseVO save(ArticleVo articleVo,
                           @RequestParam(value = "tagList[]",required =false) Long[] tagList,
                           @RequestParam(value="editImages[]",required = false) String[] editImages){
        Assert.notNull(articleVo,"请求参数缺失");
        if (null == tagList || tagList.length <= 0) {
            return ResultUtil.error("请求参数错误");
        }
       if(articleVo.getId()==null) {
           articleVo.setOriginal(articleVo.isOriginal());
           articleVo.setUserId(SessionUtil.getUser().getId());
           articleVo.setRecommended(false);
           articleVo.setTop(false);
           articleService.saveArticleAndTags(articleVo, tagList);
       }else{
            articleService.updateArticleAndTags(articleVo,tagList);
       }
       if(editImages!=null) {
           SessionUtil.removeSubmitEditImages(editImages);
       }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
    @PostMapping("/update/{type}")
    public ResponseVO update(@PathVariable("type") String type, Long id){
        ArticleVo article = articleService.select(id);
        if(type.equals("top")){
            article.setTop(!article.isTop());
        }else if(type.equals("recommended")){
            article.setRecommended(!article.isRecommended());
        }else{
            return ResultUtil.error("无此项操作");
        }
        articleService.update(article);
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
    @PostMapping("listMaterial")
    public PageResult getCoverImages(Integer pageNum){
        Assert.notNull(pageNum,"pagenum不能为空");
        if(pageNum<=0){
            return ResultUtil.tablePage(null);
        }
        return ResultUtil.tablePage(articleService.listMaterial(pageNum));
    }


}
